<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="名称">
        <el-input class="selectWidth" v-model="dataForm.key" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="tableRef" :data="dataList" :height="height" :header-cell-style="tableHeaderStyle" border
      v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column prop="name" header-align="left" align="left" label="考试名称">
      </el-table-column>
      

      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="showExamUser(scope.row.id)">预览</el-button>
          <el-button type="text" size="small" @click="deleteExam(scope.row.id)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 修改试卷弹窗 -->
  <exam-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></exam-update>
   <!-- 预览考试弹窗exam-user-->
    <exam-user v-if="examUserVisible" ref="examUser" @refreshDataList="getDataList"></exam-user>

  </div>


</template>

<script>
import ExamUpdate from './exam-update'
import ExamUser from './exam-user'
export default {

  data() {
   
    return {
      tableHeaderStyle: { background: '#f5f7fa !important', height: '40px !important', padding: '4px !important' },

      height: 0,
      dataForm: {
        key: '',
        type: '',
        code: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      examUserVisible: false

    }
  },
  components: {
      ExamUpdate,
      ExamUser

  },
  activated() {
    this.getDataList()
  },
  mounted() {
    console.log(document.body.clientHeight)
    // 至于减去多少，这个要看你的页面高度和你想将滚动轴固定在哪里了，数值可以更换
    this.$nextTick(() => {
      let tableHeight = document.body.clientHeight - 315
      this.height = tableHeight
    })
  },
  methods: {
    showExamUser(id){
      this.examUserVisible = true
      this.$nextTick(() => {
        this.$refs.examUser.init(id)
      })
    },
    repairTable() {
      this.$nextTick(() => {
        // _self.$refs.你的el-table的ref.doLayout()
        this.$refs.tableRef.doLayout()
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let search = {
        'page': this.pageIndex,
        'limit': this.pageSize,
        'key': this.dataForm.key,
      }
      this.$http({
        url: this.$http.adornUrl('/class/exam/list'),
        method: 'get',
        params: this.$http.adornParams(search)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.repairTable()
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(item) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(item.id)
      })
    },
    //删除
    deleteExam(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
            url: this.$http.adornUrl('/class/exam/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
      }).catch(() => { })
    }
  }
}
</script>
